package com.suli.account.controller;

import com.scenic.util.ResponseResult;
import com.suli.account.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author admin
 * @version 1.0 <br/>
 * <p style="font-family:SimSun;font-weight:bold">时间:<br/>2022/4/14 11:45</p>
 */
@RestController
@CrossOrigin
@SuppressWarnings("all")
@Slf4j
@RequestMapping("/analysis")
public class AnalysisData {

    @Autowired
    UserRoleService roleService;
    @Autowired
    UserTicketService ticketService;
    @Autowired
    ArticleService articleService;
    @Autowired
    ScenicService scenicService;
    @Autowired
    ScenicTicketService scenicTicketService;
    @Autowired
    RidershipService ridershipService;

    /**
     * 根据账户类型获取账户总数量
     * @param accountType 账户类型
     * @return 总数
     */
    @GetMapping("/getAccountNum/{accountType}")
    public ResponseResult getAccountNumByType(@PathVariable String accountType){
        Map<String, Object> accountByType = roleService.getAccountByType(accountType);
        return ResponseResult.ok().data(accountByType);
    }

    /**
     * 根据门票状态获取门票数量
     * @param ticketStatus
     * @return
     */
    @GetMapping("/getTicketNum/{ticketStatus}")
    public ResponseResult getTicketCountByStatus(@PathVariable String ticketStatus){
        Map<String, Object> countByType = ticketService.getCountByType(ticketStatus);
        return ResponseResult.ok().data(countByType);
    }

    /**
     * 根据文章类型获取文章数量
     * @param articleType 文章类型
     * @return 文章数量
     */
    @GetMapping("/getArticleNum/{articleType}")
    public ResponseResult getArticleCountByStatus(@PathVariable String articleType){
        Map<String, Object> articleCountByType = articleService.getArticleCountByType(articleType);
        return ResponseResult.ok().data(articleCountByType);
    }

    /**
     * 获取景点数量
     * @return
     */
    @GetMapping("/getScenicNum")
    public ResponseResult getScenicCount(){
        Map<String, Object> count = scenicService.getScenicCount();
        return ResponseResult.ok().data(count);
    }

    /**
     * 根据景点类型获取景点数量
     * @param scenicType
     * @return
     */
    @GetMapping("/getScenicNumByType/{scenicType}")
    public ResponseResult getScenicCountByStatus(@PathVariable String scenicType){
        Map<String, Object> countByType = scenicService.getCountByType(scenicType);
        return ResponseResult.ok().data(countByType);
    }

    /**
     * 获取某个时间段的门票销售量
     * @param startTime 起始时间
     * @param endTime 结束时间
     * @return 销量
     */
    @GetMapping("/getSaleTime/{startTime}/{endTime}")
    public ResponseResult getSalesVolume(@PathVariable Date startTime,@PathVariable Date endTime){
        Map<String, Object> saleByTime = ticketService.getSaleByTime(startTime, endTime);
        return ResponseResult.ok().data(saleByTime);
    }

    /**
     * 获取某个时间段的门票销售额
     * @param startTime 起始时间
     * @param endTime 结束时间
     * @return 销量
     */
    @GetMapping("/getSale/{startTime}/{endTime}")
    public ResponseResult getSale(@PathVariable Date startTime,@PathVariable Date endTime){
        Map<String, Object> saleByTime = ticketService.getSaleInTime(startTime, endTime);
        return ResponseResult.ok().data(saleByTime);
    }

    /**
     * 获取某时间段内的销量和营业额
     * @return
     */
    @GetMapping("/getSaleAndNum/{startTime}/{endTime}")
    public ResponseResult getSaleAndNumForSevenDays(@PathVariable String startTime,@PathVariable String endTime){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date start = null;
        Date end = null;
        try {
            start = sdf.parse(startTime);
            end = sdf.parse(endTime);
            Map<String, Object> sevenDays = scenicTicketService.getBetweenDays(start,end);
            if(!ObjectUtils.isEmpty(sevenDays))
            return ResponseResult.ok().data(sevenDays);
            return ResponseResult.error().message("未查到数据");
        } catch (ParseException e) {
            e.printStackTrace();
            return ResponseResult.error().message("未查到数据");
        }
    }
    @GetMapping("/getSaleQuantityRate/{startTime}/{endTime}")
    public ResponseResult getRate(@PathVariable String startTime,@PathVariable String endTime){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date start = null;
        Date end = null;
        try {
            start = sdf.parse(startTime);
            end = sdf.parse(endTime);
            Map<String, Object> salesAndRate = scenicTicketService.getSalesAndRate(start, end);
            if(!ObjectUtils.isEmpty(salesAndRate))
            return ResponseResult.ok().data(salesAndRate);
            return ResponseResult.error().message("未查到数据");
        } catch (ParseException e) {
            e.printStackTrace();
            return ResponseResult.error().message("未查到数据");
        }
    }
    @GetMapping("/getRidershipRate/{startTime}/{endTime}")
    public ResponseResult getRidershipRate(@PathVariable String startTime,@PathVariable String endTime){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date start = null;
        Date end = null;
        try {
            start = sdf.parse(startTime);
            end = sdf.parse(endTime);
            Map<String, Object> ridership = ridershipService.getRidership(start, end);
            if(!ObjectUtils.isEmpty(ridership))
            return ResponseResult.ok().data(ridership);
            return ResponseResult.error().message("未查到数据");
        } catch (ParseException e) {
            e.printStackTrace();
            return ResponseResult.error().message("未查到数据");
        }
    }
}
